package com.bwie.gddproject.net;

import com.bwie.gddproject.utils.ApiUtils;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 我走路带风 on 2017/12/13.
 */

public class NetUtil {
    private static OkHttpClient okHttpClient;
    private static ResponseService responseService;

    static {
        initOkHttpClient();
    }

    private static void initOkHttpClient() {
        if (okHttpClient == null) {
            synchronized (NetUtil.class) {
                okHttpClient = new OkHttpClient.Builder()
                        .addInterceptor(new MyIntercepter())
                        .build();
            }
        }
    }

    public static ResponseService getResponseService(){
        if (responseService == null){
            synchronized (ResponseService.class){
                if (responseService == null){
                    responseService = onCreate(ResponseService.class, ApiUtils.BASE_HOST_URL);
                }
            }
        }
        return responseService;
    }


    private static <T> T onCreate(Class<T> tClass,String url){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        return retrofit.create(tClass);
    }
}
